package com.weimei.conversionTreasure.weight;

import android.content.Context;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.BottomPopupView;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemScrollListener;
import com.weigan.loopview.OnItemSelectedListener;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.utils.ValidateUtils;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class BottomWheelPop extends BottomPopupView {

    private Click mClick;
    private TextView cancel,sure;
    private List<String> mList = new ArrayList<>();

    LoopView loopView;
    private String sel;

    public interface Click{
        void click(String s);
    }


    public BottomWheelPop(@NonNull Context context, List<String> list, Click click) {
        super(context);
        this.mClick = click;
        mList.clear();
        mList.addAll(list);
        sel = mList.get(1);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_bottom;
    }

    @Override
    protected void onCreate() {
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        loopView = findViewById(R.id.loopView);

        cancel.setOnClickListener(view ->{
            dismiss();
        });
        sure.setOnClickListener(view ->{
            if (ValidateUtils.isNotEmptyString(sel)){
                mClick.click(sel);
                dismiss();
            }else {
                ToastUtils.showShort("请选择");
            }

        });

        initLoopView(loopView);
    }

    private void initLoopView(LoopView loopView) {
        //滚动监听
        loopView.setListener(index -> {
            sel = mList.get(index);
        });
        //设置原始数据
        loopView.setItems(mList);
        //设置初始位置
        loopView.setInitPosition(1);

    }


}
